
package com.ifcar.app.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.ifcar.app.bean.PhotosetListInfo;
import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.util.HttpUtils;
import com.ifcar.app.util.NeteaseHttpClient;
import com.ifcar.app.util.StringUtil;

public class PictureSet {
    
    public static String Normal_Channle = "0096";
    
    public static String Olympic_Channle = "0005";
    
    public static String Main_Photo_Column = "4GJ60096";
    
    public static String Olympic_Photo_Column1 = "514E0005";
    
    public static String Olympic_Photo_Column2 = "514F0005";
    
    public static String Olympic_Photo_Column3 = "514G0005";
    
    
    /**
     * 获取本地图集
     * 
     * @param db
     */
    public static List<PhotosetListInfo> getPhotosListFromDB(SQLiteDatabase db,String photoColumn) {
        List<PhotosetListInfo> data = new ArrayList<PhotosetListInfo>();

        String sql = "select * from " + NewsSQLiteOpenHelper.PHOTO_LIST_TABLE + " where "+NewsSQLiteOpenHelper.PHOTO_COLUMN+"='"+photoColumn+"' group by "
                + NewsSQLiteOpenHelper.PHOTO_SETID + " order by "
                + NewsSQLiteOpenHelper.PHOTO_SETID + " desc limit 0,10";
        Cursor cursor = db.rawQuery(sql, null);

        if (cursor != null) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                PhotosetListInfo photosetinfo = new PhotosetListInfo();
                photosetinfo.setSetid(cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.PHOTO_SETID)));
                photosetinfo.setSetname(cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.PHOTO_SETNAME)));
                photosetinfo.setClientcover(cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.PHOTO_CLIENTCOVER)));
                photosetinfo.setImgsum(cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.PHOTO_IMGSUM)));
                photosetinfo.setColumn(photoColumn);

                data.add(photosetinfo);
            }
            cursor.close();
        }
        return data;
    }

    /**
     * 刷新图集信息
     * 
     * @param context
     * @param db
     * @param maxSetId
     * @param photoColumn
     * @return
     */
    public static List<PhotosetListInfo> getPhotosList(Context context, SQLiteDatabase db,
            String maxSetId,String photoColumn,String photoChannel) {
        String newestphotolisturl;
        List<PhotosetListInfo> newestphotosetlist = null;
        if (TextUtils.isEmpty(maxSetId)) {
            newestphotolisturl = String.format(StringUtil.PHOTOSETLISTURL, photoChannel, photoColumn);
        } else {
            newestphotolisturl = String.format(StringUtil.PHOTOSETNEWLISTURL, photoChannel, photoColumn,maxSetId);
        }
        String rst = HttpUtils.getResponseFromGetUrl(newestphotolisturl, 30000, context);
        if (TextUtils.isEmpty(rst)) {
            return newestphotosetlist;
        }

        try {
            JSONArray jsonArray = new JSONArray(rst);
            newestphotosetlist = new ArrayList<PhotosetListInfo>();
            if (null != jsonArray && jsonArray.length() > 0) {
                final int length = jsonArray.length();
                for (int index = 0; index < length; index++) {

                    PhotosetListInfo photosetinfo = new PhotosetListInfo();
                    JSONObject jobj = jsonArray.getJSONObject(index);

                    photosetinfo.setCover(jobj.getString(PhotosetListInfo.COVER));
                    photosetinfo.setColumn(photoColumn);
                    photosetinfo.setSetid(jobj.getString(PhotosetListInfo.SETID));
                    photosetinfo.setImgsum(jobj.getString(PhotosetListInfo.IMGSUM));
                    photosetinfo.setSetname(jobj.getString(PhotosetListInfo.SETNAME));
                    photosetinfo.setScover(jobj.getString(PhotosetListInfo.SCOVER));
                    photosetinfo.setTcover(jobj.getString(PhotosetListInfo.TCOVER));
                    photosetinfo.setClientcover(jobj.getString(PhotosetListInfo.CLIENTCOVER));
                    photosetinfo.setDatetime(jobj.getString(PhotosetListInfo.DATETIME));

                    newestphotosetlist.add(photosetinfo);

                }
            }

        } catch (Exception ex) {
            if(null!=newestphotosetlist){
                newestphotosetlist.clear();
            }
            NeteaseHttpClient.removeCache(context, newestphotolisturl);
        }
        
        if(null!=newestphotosetlist){
            
            final int count = newestphotosetlist.size();
            if (count > 0) {
                db.beginTransaction();
                try {
                    PhotosetListInfo photosetinfo = newestphotosetlist.get(0);

                    int start = 0;
                    if (!TextUtils.isEmpty(maxSetId) && maxSetId.equals(photosetinfo.getSetid())) {
                        // 说明没有断层
                        start = 1;
                    } else {
                        db.execSQL("delete from " + NewsSQLiteOpenHelper.PHOTO_LIST_TABLE);
                    }

                    for (int i = start; i < count; i++) {
                        photosetinfo = newestphotosetlist.get(i);
                        DBUtils.insertDataToPhotoListTable(db, photosetinfo.getColumn(),photosetinfo.getSetid(),
                                photosetinfo.getSetname(), photosetinfo.getClientcover(),
                                photosetinfo.getImgsum(), photosetinfo.getDatetime().replace(".0", ""),
                                true);
                    }
                    db.setTransactionSuccessful();
                } catch (Exception e) {
                } finally {
                    db.endTransaction();
                }

                if (!TextUtils.isEmpty(maxSetId)) {
                    // 当maxSetId为空时返回的图集id是由大到小排序,其它情况返回的图集id是由小到大排序,应该倒序
                    Collections.reverse(newestphotosetlist);
                }
                StringUtil.updateRefreshTime(context, photoColumn);
            }
        }
        

        return newestphotosetlist;

    }

    /**
     * 获取更多图集
     * 
     * @author wjying
     */
    public static List<PhotosetListInfo> getPhotosListMore(Context context, String minSetId,String photoColumn,String photoChannel) {
        List<PhotosetListInfo> newestphotosetlist = null;

        if (TextUtils.isEmpty(minSetId)) {
            return newestphotosetlist;
        }
        String morephotolisturl = String.format(StringUtil.PHOTOSETMORELISTURL, photoChannel , photoColumn,minSetId);
        String rst = HttpUtils.getResponseFromGetUrl(morephotolisturl, 30000, context);
        if (TextUtils.isEmpty(rst)) {
            return newestphotosetlist;
        }

        try {
            JSONArray jsonArray = new JSONArray(rst);
            newestphotosetlist = new ArrayList<PhotosetListInfo>();
            if(null!=jsonArray){
                final int length = jsonArray.length();
                for (int index = 0; index < length; index++) {
                    
                    PhotosetListInfo photosetinfo = new PhotosetListInfo();
                    JSONObject jobj = jsonArray.getJSONObject(index);

                    photosetinfo.setCover(jobj.getString(PhotosetListInfo.COVER));
                    photosetinfo.setColumn(photoColumn);
                    photosetinfo.setSetid(jobj.getString(PhotosetListInfo.SETID));
                    photosetinfo.setImgsum(jobj.getString(PhotosetListInfo.IMGSUM));
                    photosetinfo.setSetname(jobj.getString(PhotosetListInfo.SETNAME));
                    photosetinfo.setScover(jobj.getString(PhotosetListInfo.SCOVER));
                    photosetinfo.setTcover(jobj.getString(PhotosetListInfo.TCOVER));
                    photosetinfo.setClientcover(jobj.getString(PhotosetListInfo.CLIENTCOVER));
                    photosetinfo.setDatetime(jobj.getString(PhotosetListInfo.DATETIME));

                    newestphotosetlist.add(photosetinfo);
                }
            }

        } catch (Exception e) {
            if(null!=newestphotosetlist){
                newestphotosetlist.clear();
            }
            NeteaseHttpClient.removeCache(context, morephotolisturl);
        }

        return newestphotosetlist;
    }
}
